// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import mainManage from "../../framework/mainManage";
import sceneMediator from "../../framework/sceneMediator";
import gameData from "../gameData";
import { SDKManager } from "../SDKManager/SDKManager";

const {ccclass, property} = cc._decorator;

let petSkinSpine = ["cw01","cw02","cw03","cw04","cw05","cw06","cw07","cw08","cw09","cw10","cw11","cw12","cw13","cw14","cw15","cw16","cw17","cw18","cw19","cw20","cw21","cw22","cw23","cw24","cw25","cw26","cw27","cw28","cw29","cw30","cw31"];

@ccclass
export default class reliveView extends cc.Component {

    @property(cc.Node)
    roleNode : cc.Node = null;

    @property(cc.Node)
    petGroup : cc.Node = null;

    @property(cc.Node)
    videoPic : cc.Node = null;

    seeVideo : boolean = true;

    onLoad () {
        this.showRole();
        this.showPet();
    }

    btnClick(event,data){

        if(parseInt(data) == 1){
            this.node.destroy();
            if(this.callback != null){
                this.callback();
            }
        }else if(parseInt(data) == 2){

            let isSeeVideo = false;

            SDKManager.showRewardVideo("复活界面",()=>{

                isSeeVideo = true;

                gameData.failGameData++;

                gameData.isRelive = true;
                sceneMediator.LoadCurScene();
                mainManage.createToast("kebangkitan berhasil");

            },()=>{
                if(isSeeVideo){
                    mainManage.createToast("Hadiahnya akan diberikan setelah menonton video ini");
                }else{
                    mainManage.createToast("Tidak ada iklan yang tersedia saat ini");
                }
            })

        }
    }

    callback = null;
    setCallBack(callback){
        this.callback = callback;
    }


    showPet(){
        if(gameData.getPetCurSkin().length == 0){
            this.petGroup.getChildByName("pet1").active = false;
            this.petGroup.getChildByName("pet2").active = false;
        }else if(gameData.getPetCurSkin().length == 1){
            this.petGroup.getChildByName("pet1").active = true;
            this.loadPetSkin(this.petGroup.getChildByName("pet1").getChildByName("spine"),petSkinSpine[gameData.getPetCurSkin()[0]]);
            this.petGroup.getChildByName("pet2").active = false;
        }else{
            this.petGroup.getChildByName("pet1").active = true;
            this.petGroup.getChildByName("pet2").active = true;
            this.loadPetSkin(this.petGroup.getChildByName("pet1").getChildByName("spine"),petSkinSpine[gameData.getPetCurSkin()[0]]);
            this.loadPetSkin(this.petGroup.getChildByName("pet2").getChildByName("spine"),petSkinSpine[gameData.getPetCurSkin()[1]]);
        }
    }

    loadPetSkin(node,name){
        cc.loader.loadRes("anim/" + name,sp.SkeletonData,(error, resource) =>{
            let spk = node.getComponent(sp.Skeleton);
            spk.skeletonData = resource;
            node.active = true;
            spk.setAnimation(0,"liulei",true);
        })
    }

    showRole(){
        let curSkin = -1;
        console.log(gameData.randomSkinIndex);
        if(gameData.randomSkinIndex != -1){
            curSkin = gameData.randomSkinIndex;
        }else{
            curSkin = gameData.getCurUseRoleSkin();
        }
        if(curSkin < 9){
            cc.loader.loadRes("anim/zhujue",sp.SkeletonData,(err,res)=>{
                if(err){
                    console.log(err);
                    return
                }
                let spk = this.roleNode.getComponent(sp.Skeleton);
                spk .skeletonData = res;
                spk.setSkin("pf0" +  (curSkin + 1));
                spk.setAnimation(0,"animation",true);
            });
        }else{
            if(curSkin == 20){
                if(gameData.lastColorfulList[2] == 0){
                    cc.loader.loadRes("anim/zhujue22_1",sp.SkeletonData,(err,res)=>{
                        if(err){
                            console.log(err);
                            return
                        }
                        let spk = this.roleNode.getComponent(sp.Skeleton);
                        spk.skeletonData = res;
                        spk.setAnimation(0,"animation",true);
                    })
                }else if(gameData.lastColorfulList[2] == 1){
                    cc.loader.loadRes("anim/zhujue22_2",sp.SkeletonData,(err,res)=>{
                        if(err){
                            console.log(err);
                            return
                        }
                        let spk = this.roleNode.getComponent(sp.Skeleton);
                        spk.skeletonData = res;
                        spk.setAnimation(0,"animation",true);
                    })
                }
            }else if(curSkin >= 12){
                cc.loader.loadRes("anim/zhujue" + (curSkin + 2),sp.SkeletonData,(err,res)=>{
                    if(err){
                        console.log(err);
                        return
                    }
                    let spk = this.roleNode.getComponent(sp.Skeleton);
                    spk .skeletonData = res;
                    if(curSkin == 12){
                        spk.setSkin("pfi" + (gameData.lastColorfulPart + 1));
                    }else if(curSkin == 14){
                        spk.setSkin("pf1");
                    }else if(curSkin == 16){
                        spk.setSkin("pifu" + (gameData.lastColorfulList[1] + 1));
                    }
                    spk.setAnimation(0,"animation",true);
                })
            }else{
                cc.loader.loadRes("anim/zhujue",sp.SkeletonData,(err,res)=>{
                    if(err){
                        console.log(err);
                        return
                    }
                    let spk = this.roleNode.getComponent(sp.Skeleton);
                    spk .skeletonData = res;
                    spk.setSkin("pf" +  (curSkin + 1));
                    spk.setAnimation(0,"animation",true);
                });
            }
        }
    }
}
